package com.hanlink.dsp.util;

import android.content.Context;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.provider.MediaStore;

import com.edge.pbxdsp.R;
import com.hanlink.dsp.model.Mp3Info;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015/10/17
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015/10/17        PeterVon       初始创建
 *
 * </pre>
 */
public class SoundUtil {
    private static SoundPool mSoundPool;

    public static int SOUND_SWIP;
    public static int SOUND_ROTATE;
    public static int SOUND_CHANGE;


    public static void playSound(int id) {
        mSoundPool.play(id, 1.0f, 1.0f, 1, 0, 1.0f);
    }


    public static void initialize(Context context) {
        mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        SOUND_SWIP = mSoundPool.load(context, R.raw.swap_units, 1);
        SOUND_ROTATE = mSoundPool.load(context, R.raw.category_rotated, 1);
        SOUND_CHANGE = mSoundPool.load(context, R.raw.mode_selection_view_shown, 1);
    }

    /**
     * 用于从数据库中查询歌曲的信息，保存在List当中
     *
     * @return
     */
    public static List<Mp3Info> getMp3Infos(Context context) {
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
        for (int i = 0; i < cursor.getCount(); i++) {
            Mp3Info mp3Info = new Mp3Info();
            cursor.moveToNext();
            int isMusic = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
            String url = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA));
            if (isMusic != 0) {        //只把音乐添加到集合当中  && url.endsWith(".mp3")
                mp3Info.setMediaId(cursor.getLong(cursor
                        .getColumnIndex(MediaStore.Audio.Media._ID)));//音乐id
                mp3Info.setTitle(cursor.getString((cursor
                        .getColumnIndex(MediaStore.Audio.Media.TITLE))));//音乐标题
                mp3Info.setArtist(cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST)));//艺术家
                mp3Info.setDuration(cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DURATION)));//时长
                mp3Info.setSize(cursor.getLong(cursor
                        .getColumnIndex(MediaStore.Audio.Media.SIZE)));//文件大小
                mp3Info.setUrl(url);//文件路径
                mp3Infos.add(mp3Info);
            }
        }
        cursor.close();
        return mp3Infos;
    }

    public static short getColor(int color) {
        if (Math.abs(color) > 255)
            return 255;
        else return (short) Math.abs(color);
    }


}
